package com.hn.shopadmin.service.impl;

import com.hn.kiss.modules.sys.shopdb.entity.RoleJurisdiction;
import com.hn.kiss.modules.sys.shopdb.entity.RoleJurisdictionExample;
import com.hn.kiss.modules.sys.shopdb.mapper.RoleJurisdictionMapper;
import com.hn.shopadmin.dao.RoleJursdinctionDao;
import com.hn.shopadmin.dto.JursdictionAllRoleDTO;
import com.hn.shopadmin.service.RoleJurisdictionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleJurisdictionServiceimpl implements RoleJurisdictionService {
    @Autowired
    private RoleJursdinctionDao roleJursdinctionDao;
    @Autowired
    private RoleJurisdictionMapper roleJurisdictionMapper;

    @Override
    public int insert(String roleCode, String jurisdictionId) {
        RoleJurisdiction roleJurisdiction = new RoleJurisdiction();
        roleJurisdiction.setRoleCode(roleCode);
        roleJurisdiction.setJurisdictionId(jurisdictionId);
        return roleJurisdictionMapper.insert(roleJurisdiction);
    }

    @Override
    public int deleteByRoleCode(String roleCode) {
        RoleJurisdictionExample roleJurisdictionExample = new RoleJurisdictionExample();
        roleJurisdictionExample.createCriteria().andRoleCodeEqualTo(roleCode);
        return roleJurisdictionMapper.deleteByExample(roleJurisdictionExample);
    }

    @Override
    public int deleteByJurisdictionId(String jurisdictionId) {
        RoleJurisdictionExample roleJurisdictionExample = new RoleJurisdictionExample();
        roleJurisdictionExample.createCriteria().andJurisdictionIdEqualTo(jurisdictionId);
        return roleJurisdictionMapper.deleteByExample(roleJurisdictionExample);
    }

    @Override
    public int allocationRoleJurisdiction(String roleCode, String[] jurisdictionIds) {
        int count = 0;
        deleteByRoleCode(roleCode);

        for (String jurisdictionId : jurisdictionIds) {
            count += insert(roleCode, jurisdictionId);
        }
        return count;
    }

    @Override
    public List<JursdictionAllRoleDTO> listJurisdictionAllRoleCode(String roleCode) {
      return roleJursdinctionDao.selectJursdinctionAllVsRole(roleCode);
    }
}
